package com.joysuch.wwyt.bp.service.impl;

import com.joysuch.wwyt.bp.entity.BpDepart;
import com.joysuch.wwyt.bp.repository.BpDepartDao;
import com.joysuch.wwyt.bp.service.BpRiskCommonService;
import com.joysuch.wwyt.core.entity.BaseUser;
import com.joysuch.wwyt.core.repository.BaseUserDao;
import com.joysuch.wwyt.core.support.Context;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author ZhangDong
 * @Date 2020/4/14 18:18
 */
@Service
@Transactional
@Slf4j
public class BpRiskCommonServiceImpl implements BpRiskCommonService {

    @Autowired
    private BaseUserDao baseUserDao;
    @Autowired
    private BpDepartDao bpDepartDao;

    /**
     * >=总经理
     */
    @Override
    public boolean managerOrNot() {
        Long currentUserId = Context.getCurrentUserId();
        BaseUser baseUser = baseUserDao.getOne(currentUserId);
        Long departId = baseUser.getDepartId();
        //当前部门
        BpDepart depart = bpDepartDao.getOne(departId);
        String departName = depart.getName();
        if (departName != null && (departName.contains("总经理")
                || departName.contains("董事长") || depart.getParentId() == null)) {
            return true;
        }
        //上一级
        BpDepart departU1 = bpDepartDao.getOne(depart.getParentId());
        if (departU1.getParentId() == null) {
            return true;
        }
        //上二级
        BpDepart departU2 = bpDepartDao.getOne(departU1.getParentId());
        if (departU2.getParentId() == null) {
            return true;
        }
        return false;
    }
}
